package org.itboys.mobile.service.mongo.front;

import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayHelpCenterDto;
import org.itboys.mobile.entity.mongo.SundayAdvertisement;
import org.itboys.mobile.entity.mongo.SundayAdvertisementZone;
import org.itboys.mobile.service.mongo.web.SundayAdvertisementService;
import org.itboys.mobile.service.mongo.web.SundayAdvertisementZoneService;
import org.itboys.mongodb.core.MongoDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyang on 2018/1/17.
 * 圣代_帮助中心_service
 */
@Service
public class SundayFrontHelpService{

    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;


    @Autowired
    private SystemDictionaryService systemDictionaryService;
    @Autowired
    private SundayAdvertisementService advertisementService;
    @Autowired
    private SundayAdvertisementZoneService advertisementZoneService;


    /**
     * 查询维护的帮助中心
     * @return
     */
    public SundayHelpCenterDto select(){
        //大类别
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("desc","帮助中心类别");
        List<SundayAdvertisementZone> advertisementZoneList = advertisementZoneService.list(param);

        //小类别
        param.clear();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("viceName","帮助中心类别");
        List<SundayAdvertisement> advertisementList = advertisementService.list(param);

        String key = "";
        if(ListUtil.isNotNull(advertisementZoneList)){
            key = advertisementZoneList.get(0).getKey();
        }
        SundayHelpCenterDto dto = new SundayHelpCenterDto(advertisementZoneList,advertisementList,key);
        return dto;
    }

    /**
     * 不分页获取帮助中心数据
     * @return
     */
    public Map<String,Object> getIndexData(){
        Map<String,Object> result = new HashMap<String,Object>();
        //获取所有的帮助中心大类别
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("contains:key","help_type");
        List<SundayAdvertisementZone> zones = advertisementZoneService.list(param);

        result.put("zones",zones);
        List<String> keys = new ArrayList<>();
        for(SundayAdvertisementZone zone:zones){
            keys.add(zone.getKey());
        }
        //小类别
        param.clear();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("zoneKey in",keys);
        List<SundayAdvertisement> advertisements = advertisementService.list(param);

        result.put("advertisements",advertisements);
        return result;
    }
}
